package com.leapp.sk.viewdelegate.adapter.task

import android.graphics.drawable.AnimationDrawable
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import cc.vv.lkimcomponent.lkim.bean.LKIMMessage
import com.example.mvp.delegate.BaseAdapterDelegate
import com.leapp.sk.R
import com.leapp.sk.bean.CompleteTaskEntity
import com.leapp.sk.util.im.IMVoicePlayer

/**
 * 语音作业适配器视图托管
 * @author  chaohaipeng  on 2018/8/3.
 */
class VoiceTaskAdapterDelegate  : BaseAdapterDelegate(){
    override fun getOptionsMenuId(): Int {
        return 0
    }

    override fun initWidget() {
    }

    override fun onDestroy() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.adapter_task_voice
    }

    /**
     * 设置语音时长
     */
    fun setData(data: CompleteTaskEntity,isdetial:Boolean){
        val iv_atv_delete_voice=  getView<ImageView>(R.id.iv_atv_delete_voice)
        if (isdetial){
            iv_atv_delete_voice.visibility=View.GONE
        }else{
            iv_atv_delete_voice.visibility=View.VISIBLE
        }
      val tv_atv_voice_time=  getView<TextView>(R.id.tv_atv_voice_time)
        tv_atv_voice_time.text=""+data.duration+"\""
        initVoice(data)
    }

    /**
     * 初始化语音数据
     */
    private  fun initVoice(data: CompleteTaskEntity){
        val iv_atv_voice_image = getView<ImageView>(R.id.iv_atv_voice_image)
        val rl_atv_voice = getView<RelativeLayout>(R.id.rl_atv_voice)
        rl_atv_voice.setOnClickListener(View.OnClickListener {
            voicePlayer(iv_atv_voice_image, data, data.url)
        })
    }



    /**
     * 语音播放
     */
    fun voicePlayer(voiceImage: ImageView, data: CompleteTaskEntity, donwLoadVoce:String){

        IMVoicePlayer.getInstance().playUrl(donwLoadVoce, false, object : IMVoicePlayer.PlayerInter {
            override fun onStart() {
                voiceImage.setImageResource(R.drawable.animation_imreceive_voice_play)
                val animationDrawable = voiceImage.getDrawable() as AnimationDrawable
                animationDrawable?.start()
                data.isPalyer=true
            }
            override fun onCompletion() {
                val animationDrawable = voiceImage.getDrawable() as AnimationDrawable
                animationDrawable?.stop()
                voiceImage.setImageResource(R.drawable.animation_imreceive_voice_play)
                data.isPalyer=false
            }

            override fun onError() {
                val animationDrawable = voiceImage.getDrawable() as AnimationDrawable
                animationDrawable?.stop()
                voiceImage.setImageResource(R.drawable.animation_imreceive_voice_play)
                data.isPalyer=false
            }

            override fun onStop() {
                val animationDrawable = voiceImage.getDrawable() as AnimationDrawable
                animationDrawable?.stop()
                voiceImage.setImageResource(R.drawable.animation_imreceive_voice_play)
                data.isPalyer=false
            }
        })


    }

}